package top.luoyuanxiang.mqtt.utils;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SM4;

import java.nio.charset.StandardCharsets;
import java.util.Random;

public class KeyGenerateUtil {

    /**
     * sm4加密
     *
     * @param keyHex key的Hex字符串
     * @param data   待加密的字符串
     * @return 返回加密后的Hex字符串
     */
    public static String sm4EncryptHex(String keyHex, String data) {
        SM4 sm4 = new SM4(HexUtil.decodeHex(keyHex));
        return sm4.encryptHex(data.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * sm4加密
     *
     * @param keyHex  key的Hex字符串
     * @param dataHex 待加密的字符串
     * @return 返回加密后的Hex字符串
     */
    public static String sm4Decrypt(String keyHex, String dataHex) {
        SM4 sm4 = new SM4(HexUtil.decodeHex(keyHex));
        return sm4.decryptStr(dataHex);
    }

    /**
     * sm4加密
     *
     * @param keyHex  key的Hex字符串
     * @param dataBytes 待加密的字符串
     * @return 返回加密后的Hex字符串
     */
    public static byte[] sm4Decrypt(String keyHex, byte[] dataBytes) {
        SM4 sm4 = new SM4(HexUtil.decodeHex(keyHex));
        return sm4.decrypt(dataBytes);
    }

    /**
     * 加密
     *
     * @param data      数据
     * @param publicKeyHex 公钥
     * @return {@link byte[]}
     */
    public static byte[] sm2Encrypt(String publicKeyHex, byte[] data) {
        SM2 sm2 = SmUtil.sm2(null, publicKeyHex);
        return sm2.encrypt(data, KeyType.PublicKey);
    }

    /**
     * 解密
     *
     * @param data       数据
     * @param privateKeyHex 私钥
     * @return {@link byte[]}
     */
    public static byte[] sm2Decrypt(String privateKeyHex, String data) {
        SM2 sm2 = new SM2(privateKeyHex, null);
        return sm2.decryptFromBcd(data, KeyType.PrivateKey);
    }

    public static void main(String[] args) {
        System.out.println(sm4Decrypt("72bf9a0d544bba9b87bcd344f6ab65b8", "446cd1345d03da753ac4e5e690da41fd0121ce95288e6cc610f30ca7b04738f1a6ac39c49af2be50721647906b691479fbaae732a9ec403e6421c21eaf76be0cc310292670582393993ece14cb50111cf18251121244e7c7645d89f68ce114de0e9d4fdf255b9819e3946c7e50300ca9da2b0533ae308e92a2fb1317e294b433b75aad549545226eb9151a4588d1298d6d3f30cf202e1cf6fdb830c5c8410b746277e72e8328faff53b78a74714c9c60695a771d21a562c68bd98ad75bc1e09f"));

        SM2 sm2 = new SM2(null, "3059301306072a8648ce3d020106082a811ccf5501822d034200048995ea1bf413fa51bf60c2d4ffae6028abb258ea11437a066d10d20f249c28493f3cc717a52e449c3262c6f9781fb0d4a9734c0a3c4e213e34ce084c1453");
        String s = sm2.encryptHex("123456", KeyType.PublicKey);
        System.out.println(s);
    }

    /**
     * 获取16进制随机数
     *
     * @param len 长度
     * @return 16进制字符串
     */
    public static String randomHexString(int len) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < len; i++) {
            result.append(Integer.toHexString(new Random().nextInt(16)));
        }
        return result.toString().toLowerCase();
    }
}
